﻿//#define ToLua

using System.Collections.Generic;
using UnityEditor;

#if ToLua
using LuaInterface;
#endif

namespace UFrame
{
    public class BaseTestWindow : EditorWindow
    {
        public static Dictionary<string, BaseTestWindow> WinDic = new Dictionary<string, BaseTestWindow>();

#if ToLua
    public static LuaState Lua;
#endif

        public static void OpenTestWinodw<T>() where T : BaseTestWindow
        {
            BaseTestWindow win = null;
            string title = typeof(T).Name;
            if (WinDic.ContainsKey(title))
            {
                win = WinDic[title];
            }

            if (win != null)
            {
                win.Close();
                win = null;
                WinDic.Remove(title);
                return;
            }

            win = GetWindow<T>(false, title, false);
            if (WinDic.ContainsKey(title) == false)
            {
                WinDic.Add(title, win);
            }
            else
            {
                WinDic[title].Close();
                WinDic[title] = win;
            }

            win.Show();
        }


        public void CustomLuaGUI(string className)
        {
#if ToLua
        if (GUILayout.Button("加载Lua"))
        {
            Lua.DoFile(className);
            Debug.Log("刷新Lua");
        }

        if (Lua == null)
        {
            new LuaResLoader();
            Lua = new LuaState();
            LuaBinder.Bind(Lua);
            Lua.Start();
        }

        if (Lua != null)
        {
            Lua.Require(className);
            LuaFunction f = Lua.GetFunction(className + ".OnGUI");
            if (f != null)
                f.Call();
        }
#endif
        }

        protected virtual void OnApplicationQuit()
        {
#if ToLua
        Lua.Dispose();
        Lua = null;
        Debug.Log("lua销毁");
#endif
        }



        protected int IntField(string title, int val)
        {
            return EditorGUILayout.IntField(title, val);
        }

        protected string TextField(string title, string val)
        {
            return EditorGUILayout.TextField(title, val);
        }

        protected void LabelField(string title, string val)
        {
            EditorGUILayout.LabelField(title, val);
        }

    }
}